---
link: md-file/
---
<!DOCTYPE html>
<html lang="en">
<head>
    <script src="../js/jquery.min.js"></script>
</head>
<body>
<div id="show-div"></div>
<script type="module">
    function loadFile(url) {
        console.log("loadFile", url);
        return fetch(url)
            .then(response => {
                if (!response.ok) {
                    throw new LoadError(response);
                }
                return response.text();
            })
            .catch(error => {
                console.error('Error loading file', url, error);
                throw error;
            });
    }
    const baseurl = location.pathname;
    const url = baseurl.replace("{{ page.link }}", "");
    const div = $("#show-div");
    console.log("md-file loading:", url);
    if (url !== "/") {
        div.html(`
<div>
<h1>正在加载……</h1>
<p>如果长时间没有反应，请查看控制台是否报错，检查链接是否正确。</p>
</div>`);
        try {
            const data = await loadFile(url);
            const parser = new DOMParser();
            const fileDoc = parser.parseFromString(data, "text/html");
            const add = $(fileDoc).find(".add-md");
            const newDoc = $(parser.parseFromString("<!DOCTYPE html><html lang='en'><head></head><body></body></html>", "text/html"));
            const body = newDoc.find("body");
            const head = newDoc.find("head");
            add.each(function () {
                const item = $(this);
                item.removeClass("add-md");
                if (item.prop("tagName") === "head") {
                    head.append(item.html());
                } else if (item.prop("tagName") === "body") {
                    body.append(item.html());
                } else {
                    body.append(item);
                }
            });
            $("body").html(body.html());
            $("head").html(head.html());
        } catch (error) {
            console.error("loading error!", error);
            getError(error.code);
        }
    } else {
        div.html(`
<h1>md-file功能</h1>
<p>更纯洁的站内Markdown展示。</p>
`);
    }
</script>
</body>
</html>